Getting Started with Shiny

Howard Baek

Fred Hutch Data Science Lab (DaSL)

Goal of this talk

Photo by Yulia Khlebnikova on Unsplash

Agenda

  • Introduction to Shiny
  • Structure of a Shiny App
  • Demo Shiny App
  • Layouts of Shiny
  • Shiny Gallery
  • Troubleshooting
  • Sharing your Shiny App Online

Shiny

Shiny is an R package that makes it easy to build interactive web apps straight from R. No knowledge of HTML, CSS, or JavaScript is required, but it is fully customizable and extensible with these languages. Essentially, Shiny lets you write R and create web applications.

Usage of Shiny

  • Academia: Teaching tool for statistical concepts and for new statistical methods.
  • Big Pharma: Collaboration between scientists and analysts during drug development
  • Big Tech: Set up real-time metrics dashboards
  • Many others…

Structure of a Shiny App

library(shiny)

ui <- fluidPage(
  # UI code
)

server <- function(input, output) {
  # Server code
}

# Create shiny app
shinyApp(ui = ui, server = server)

Demo App: Hello Shiny!

library(shiny)

# Define UI for app that draws a histogram
ui <- fluidPage(
  
  # App title
  titlePanel("Hello Shiny!"),
  
  # Sidebar layout 
  sidebarLayout(
    
    # Sidebar panel
    sidebarPanel(
      
      # Input: Slider for the number of bins
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
      
    ),
    
    # Main panel for displaying outputs
    mainPanel(
      
      # Output: Histogram
      plotOutput(outputId = "distPlot")
      
    )
  )
)

# Define server code required to draw a histogram
server <- function(input, output) {
  
  # Histogram of the Old Faithful Geyser Data
  # with requested number of bins
  # Code that generates a histogram is wrapped in a call
  # to renderPlot.
  output$distPlot <- renderPlot({
    
    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    hist(x, breaks = bins, col = "#007bc2", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")
    
  })
  
}

# Create shiny app
shinyApp(ui = ui, server = server)

Layouts

Single Page: Sidebar layout

Single Page: Multi-row layout

Multi-page: Horizontal Tabs with tabsetPanel()

Multi-page: Vertical Tabs with navListPanel()

Multi-page: Dashboards

Troubleshooting and Sharing your Shiny App Online

Troubleshooting

Missing parenthesis, commas, and typos

library(shiny)

# Define UI for app that draws a histogram
ui <- fluidPage(
  
  # App title
  titlePanel("Hello Shiny!"),
  
  # Sidebar layout with input and output definitions
  sidebarLayout(
    
    # Sidebar panel for inputs
    sidebarPanel(
      
      # Input: Slider for the number of bins
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
      
    ),
    
    # Main panel for displaying outputs
    mainPanel(
      
      # Output: Histogram
      plotOutput(outputId = "distPlot")
      
    )
  )
)

# Define server code required to draw a histogram
server <- function(input, output) {
  
  # Histogram of the Old Faithful Geyser Data
  # with requested number of bins
  # Code that generates a histogram is wrapped in a call
  # to renderPlot.
  output$distPlot <- renderPlot({
    
    x <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    hist(x, breaks = bins, col = "#007bc2", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")
    
  })
  
}

# Create shiny app
shinyApp(ui = ui, server = server)

Share your Shiny Applications Online

shinyapps.io

  • shinyapps.io is a software as a service (SaaS) product that makes it easy to share your Shiny apps online.
  • It runs in the cloud on shared servers by Posit.
  • It has both free and paid plans.

Further Resources

Reference

Thank you!